<template>
	<view>
		<view class="all">
			<u-form-item :label-position="labelPosition" label="" label-width="150" prop="password">
				<u-input :password-icon="true" :border="border" type="text" v-model="mobile" placeholder="请输入手机号码">
				</u-input>
			</u-form-item>
			<u-form-item :label-position="labelPosition" label="验证码" prop="code" label-width="100" class="froms">
				<u-input :border="border" placeholder="请输入验证码" v-model="model.code" type="text"></u-input>
				<u-button slot="right" type="success" size="mini" @click="getCode">{{codeTips}}</u-button>
			</u-form-item>
			<u-verification-code seconds="60" ref="uCode" @change="codeChange"></u-verification-code>
		</view>
		<view class="button-bottom">
			<button class="btn btn-square btn-b" hover-class="btn-hover2" @click="submitHandler()"
				:disabled='submitStatus' :loading='submitStatus'>保存</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: '修改手机号',
				border: false,
				labelPosition: 'left',
				submitStatus:false,
				model: {
					code: ''
				},
				codeTips:'',
				border: false,
				mobile:""
			}
		},
		methods: {
			submitHandler() {
				this.submitStatus = true;
				this.$api.changePhone().then({
					mobile: this.mobile,
					verify_code: this.model.code
				}, res => {
					this.$common.successToShow(res.msg, result => {
						this.submitStatus = false;
						uni.navigateBack({
							delta: 1
						});
					});
				});
			},
			codeChange(text) {
				this.codeTips = text;
			},
			//获取验证码
			getCode() {
				let data={
					mobile:this.mobile
				}
				this.$api.code(data).then(res => {
					console.log(res)
				}, err => {
					// console.log(res)
				})
				if(this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码',
						mask: true
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						this.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			}
		}
	}
</script>

<style lang="less">
	.all {
		width: 100%;
		background: #F2F1F5;
		padding: 0 20rpx;
	}
</style>
